package 链表;

// https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer/
// [1, 0, 1] => 5
public class _1290_二进制链表转整数 {

    public static int getDecimalValue(ListNode head) {
        if (head == null) return 0;

        int value = 0;
        while (head != null) {
//            value = (value << 1) + head.val;
            value = value * 2 + head.val;
            head = head.next;
        }
        return value;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(1);
        head.next.next = new ListNode(1);
        System.out.println(head);
        int value = getDecimalValue(head);
        System.out.println(value);
    }
}
